import ttkbootstrap as ttk
from ttkbootstrap.constants import *

from modules.image_to_pdf import ImageToPDFPage
from modules.pdf_add_image import PDFAddImagePage
from modules.pdf_compress import PDFCompressPage
from modules.pdf_merge import PDFMergePage
from modules.pdf_split import PDFSplitPage


class PDFToolApp(ttk.Window):
    def __init__(self):
        super().__init__(themename="cosmo")
        self.title("PDF工具")
        self.geometry("1200x900")

        # 创建主框架
        self.main_frame = ttk.Frame(self)
        self.main_frame.pack(fill=BOTH, expand=YES, padx=10, pady=10)

        # 创建功能选择区域
        self.function_frame = ttk.Frame(self.main_frame)
        self.function_frame.pack(side=LEFT, fill=Y, padx=5)

        # 创建功能按钮
        self.function_buttons = []
        self.functions = [
            ("PDF合并", PDFMergePage),
            ("PDF拆分", PDFSplitPage),
            ("图片转PDF", ImageToPDFPage),
            ("PDF添加图片", PDFAddImagePage),
            ("PDF压缩", PDFCompressPage)
        ]

        for i, (text, _) in enumerate(self.functions):
            btn = ttk.Button(
                self.function_frame,
                text=text,
                command=lambda idx=i: self.show_function(idx),
                style="primary.TButton",
                width=15
            )
            btn.pack(pady=5, fill=X)
            self.function_buttons.append(btn)

        # 创建内容区域
        self.content_frame = ttk.Frame(self.main_frame)
        self.content_frame.pack(side=LEFT, fill=BOTH, expand=YES, padx=5)

        # 初始化所有功能页面
        self.pages = {}
        for text, page_class in self.functions:
            page = page_class(self.content_frame)
            page.pack(fill=BOTH, expand=YES)
            self.pages[text] = page
            page.pack_forget()

        # 默认显示PDF合并功能
        self.show_function(0)

    def show_function(self, index):
        # 重置所有按钮样式
        for btn in self.function_buttons:
            btn.configure(style="primary.TButton")

        # 设置当前选中按钮的样式
        self.function_buttons[index].configure(style="success.TButton")

        # 隐藏所有页面
        for page in self.pages.values():
            page.pack_forget()

        # 显示选中的页面
        text, _ = self.functions[index]
        self.pages[text].pack(fill=BOTH, expand=YES)


if __name__ == "__main__":
    app = PDFToolApp()
    app.mainloop()
